Code
# Load package(s)
library(tidyverse)
library(tigris)
library(statebins)
# Load dataset(s)
load("data/US_income.rda")Data Visualization (STAT 302)
We’ll be using the US_income.rda dataset which should be placed in the /data subdirectory in our data_vis_labs project. You’ll also be downloading your own data to build maps.
# Load package(s)
library(tidyverse)
library(tigris)
library(statebins)
# Load dataset(s)
load("data/US_income.rda")Make a county map of a US state using geom_polygon(). Maybe use your home state or a favorite state. Please do NOT use the state in the ggplot2 book example.
Optional: Consider adding major cities (or your home town).
Hints:
Now use geom_sf() instead. You’ll need to download data for this. You can use either the tigris (github page) or geodata packages. Either tigris’ counties() with cb = TRUE or geodata’s gadm() could be useful.
Using the US_income dataset, recreate the following graphics as precisely as possible.
# Setting income levels
US_income <- mutate(
US_income,
income_bins = cut(
ifelse(is.na(median_income), 25000, median_income),
breaks = c(0, 40000, 50000, 60000, 70000, 80000),
labels = c("< $40k", "$40k to $50k",
"$50k to $60k", "$60k to $70k", "> $70k"),
right = FALSE
)
)Hints:
geom_sf() — boundary color is "grey80" and size is 0.2viridis package (discrete = TRUE in scale_* function)#plot map
ggplot(US_income) +
geom_sf(aes(geometry = geometry, fill = income_bins), color = "grey80", size = 0.2) +
# create legend/scale
viridis::scale_fill_viridis(discrete = TRUE, name = "Median\nIncome") +
coord_sf() +
theme_void()Hints:
statebins::geom_statebins()viridis package (discrete = TRUE in scale_* function)ggplot(US_income, aes(state = name, fill = income_bins)) +
statebins::geom_statebins() +
viridis::scale_fill_viridis(discrete = TRUE, name = "Median\nIncome") +
theme_statebins()Pick any city or foreign country to build a map for. You can dress it up or make it as basic as you want. Also welcome to try building a graphic like that depicted at the end of section 6.5 — use a different region though.
# map of France
france_map <- map_data("france", region = ".", exact = FALSE)
#creating French capitals data frame
french_capitals <- tibble::tribble(
~city, ~lat, ~long,
"Paris", 48.8534, 2.3488,
"Marseille", 43.2965, 5.3698,
"Lyon", 45.7640, 4.8357,
"Toulouse", 43.6045, 1.4442,
"Nice", 43.7102, 7.2618,
"Nantes", 47.2184, -1.5536,
"Strasbourg", 48.5846, 7.7433,
"Montpellier",43.6108, 3.8767,
"Bordeaux", 44.8378, -0.5792,
"Lille", 50.6293, 3.0573,
"Rennes", 48.1173, -1.6743,
"Reims", 49.2583, 4.0317,
"Le Havre", 49.4938, 0.1016,
"Saint-Etienne", 45.4397, 4.3872,
"Toulon", 43.1242, 5.9306
)
#making plot
ggplot() +
geom_polygon(data = france_map, aes(x = long, y = lat, group = group),
fill = "white", color = "black") +
geom_point(data = french_capitals, aes(x = long, y = lat), color = "red", size = 2.5) +
coord_sf() +
theme_void()Optional
Using the tidycensus package and few others, try to create a map like below using these directions. Try using a different geographical area and a different variable from the ACS.